package net.example.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import net.example.model.GGUser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GGUserPersister {

    private static GGUserPersister instance =new GGUserPersister();
    private GGUserPersister(){}
    public static GGUserPersister getInstance() {
        return instance;
    }

    public List<GGUser> getAll()
    {
        SQLiteDatabase db = DatabaseHelper.getInstance().getReadableDatabase();
        ArrayList<GGUser> list = new ArrayList<GGUser>();
        Cursor cursor = db.query(DatabaseHelper.GGUser,
                new String[]{"UserID", "PasswordMD5", "Name", "Friends",
                        "CommentNames", "OrgID", "Signature", "HeadImageIndex",
                        "HeadImageData", "Groups", "CreateTime", "Version"},
                null, null, null, null, null);
        while (cursor.moveToNext()) {
            GGUser user = new GGUser();
            user.setUserID(cursor.getString(cursor.getColumnIndex("UserID")));
            user.setPasswordMD5(cursor.getString(cursor.getColumnIndex("PasswordMD5")));
            user.setName(cursor.getString(cursor.getColumnIndex("Name")));
            user.setFriends(cursor.getString(cursor.getColumnIndex("Friends")));
            user.setCommentNames(cursor.getString(cursor.getColumnIndex("CommentNames")));
            user.setOrgID(cursor.getString(cursor.getColumnIndex("OrgID")));
            user.setSignature(cursor.getString(cursor.getColumnIndex("Signature")));
            user.setHeadImageIndex(cursor.getInt(cursor.getColumnIndex("HeadImageIndex")));
            user.setGroups(cursor.getString(cursor.getColumnIndex("Groups")));
            user.setCreateTime(new Date(cursor.getLong(cursor.getColumnIndex("CreateTime"))));
            user.setVersion(cursor.getInt(cursor.getColumnIndex("Version")));
            user.setHeadImageData(cursor.getBlob(cursor.getColumnIndex("HeadImageData")));
            list.add(user);
        }

        cursor.close();//关闭结果集
        db.close();//关闭数据库对象
        return list;
    }

    public void delete(String userID)
    {
        SQLiteDatabase db = DatabaseHelper.getInstance().getWritableDatabase();
        db.delete(DatabaseHelper.GGUser, "UserID=?", new String[]{userID});
        db.close();//关闭数据库对象
    }

    public void insert(GGUser user)
    {
        SQLiteDatabase db = DatabaseHelper.getInstance().getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("UserID", user.getUserID());
        values.put("PasswordMD5", user.getPasswordMD5());
        values.put("Name", user.getName());
        values.put("Friends", user.getFriends());
        values.put("CommentNames", user.getCommentNames());
        values.put("OrgID", user.getOrgID());
        values.put("Signature", user.getSignature());
        values.put("HeadImageIndex", user.getHeadImageIndex());
        values.put("HeadImageData", user.getHeadImageData());
        values.put("Groups", user.getGroups());
        values.put("CreateTime", user.getCreateTime().getTime());
        values.put("Version", user.getVersion());
        //调用方法插入数据
        db.insert(DatabaseHelper.GGUser, "Name", values);
        db.close();
    }
}
